const state = {
  visitedViews: [],
  cachedViews: [],

}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (view.query) {
      if (view.query.tags !== undefined) {
        // 开启标签新增新逻辑
        let title = view.meta.title
        let pathId = view.query.id !== undefined ? '/' + view.query.id + '/' : ''
        if (state.visitedViews.some(v => v.path === view.path + pathId)) {
          if (state.visitedViews.some(v => v.fullPath === view.fullPath)) return
        }
        if (view.query.title !== undefined) title = '【' + view.query.title + '】' + title
        state.visitedViews.push(
            Object.assign({}, view, {
              path: view.path + pathId,
              title: title || 'no-name'
            })
        )
      } else {
        // 原来的标签新增逻辑
        if (state.visitedViews.some(v => v.path === view.path)) return
        state.visitedViews.push(
            Object.assign({}, view, {
              title: view.meta.title || 'no-name'
            })
        )
      }
    } else {
      // 原来的标签新增逻辑
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push(
          Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          })
      )
    }
	
  },



  DEL_VISITED:(state,view)=>{
	state.visitedViews = state.visitedViews.filter(item => item.fullPath !== view)
  },

  DEL_OTHER_VISITED:(state,view)=>{
    state.visitedViews = state.visitedViews.filter(item => item.fullPath === view)
  },
  
  
  DEL_ALL_VISITED_VIEWS: state => {
    // keep affix tags
    const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
    state.visitedViews = affixTags
  },
  
  
}

const actions = {
  addView({ dispatch }, view) {
	 
    dispatch('addVisitedView', view)
    //dispatch('addCachedView', view)
	
  },
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  delVisitedView({ commit }, view) {
    commit('DEL_VISITED', view)
  },
  delOtherVisitedView({ commit }, view) {
    commit('DEL_OTHER_VISITED', view)
  },
  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      //dispatch('delAllCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        //cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.visitedViews])
    })
  },
 
  
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
